<template>
	<div class="about">
		<el-page-header @back="goBack"/>
		
			<el-row>
		<el-form label-width="120px" @submit.native.prevent="save">
			<el-row style=" display: flex;justify-content:flex-start;align-items:center">
			<h1 class="h1">{{id ? '编辑':'提交'}}赛事信息</h1>
				
			<el-form-item>
				<el-button 
						size="small" :loading="uploading" type="primary"
						native-type="submit">保存</el-button>
				</el-form-item>
		</el-row>
				<el-col :span="14">
					<div class="grid-content">
						<el-form-item label="赛事名称">
							<el-input v-model="model.name"></el-input>
						</el-form-item>
						<el-form-item label="负责人姓名">
							<el-input v-model="model.principal"></el-input>
						</el-form-item>
						<el-form-item label="主办单位">
							<el-input v-model="model.organizer"></el-input>
						</el-form-item>
						<el-form-item label="决赛时间">
							<el-date-picker type="month" v-model="model.time"></el-date-picker>
						</el-form-item>
						<el-form-item label="赛事简介" style="height:25rem">
							<el-input type="textarea" v-model="model.intro" style="width:25rem"
							:autosize="{ minRows: 2, maxRows: 6}"
							maxlength="200" show-word-limit/>
						</el-form-item>
					</div>
				</el-col>
				<el-col :span="10">
					<div class="grid-content">
						<el-form-item label="赛事Logo">
							<el-upload class="avatar-uploader" :action=qiniuUrl	:http-request = this.$qiniu
								:headers="getAuthHeaders()" :show-file-list="false"
								:before-upload="beforeAvatarUpload">
								<img v-if="model.logo" :src="model.logo" class="avatar">
								<i v-else class="el-icon-plus avatar-uploader-icon"></i>
								<transition v-if="uploading" name="el-fade-in-linear">
								<el-progress :percentage="percentage"/>
								</transition>
							</el-upload>
						</el-form-item>

						<el-form-item label="审核">
							<el-radio-group v-model="model.check">
								<el-radio-button label="通过"></el-radio-button>
								<el-radio-button label="不通过"></el-radio-button>
								</el-radio-group>
						</el-form-item>

						<el-form-item>
							<el-button type="primary" native-type="submit">保存</el-button>
						</el-form-item>
					</div>
				</el-col>
			
		</el-form>
			</el-row>
		<el-dialog :visible.sync="dialogVisible">
			<img width="100%" :src="dialogImageUrl" alt="">
		</el-dialog>
	</div>
</template>

<script>
	export default {
		props: {
			id: {}
		},
		data() {
			return {
				model: {
					name: '',
					logo: '',
					principal: '',
					oganizer: '',
					time: '',
					intro: '',
					check: ''
				},
				dialogImageUrl: '',
				dialogVisible: false,
				uploading:false,
				percentage:0,
				//这是根据你七牛云创建空间时所选择的地区
				qiniuUrl : 'https://upload-z2.qiniup.com'
			}
		},
		methods: {
			goBack() {
				this.$router.go(-1)
			},
			async save() {
				let res
				if (this.model.name == null || this.model.name.length == 0) {
					this.$message({
						type: 'warning',
						message: '内容不能为空'
					})
				} else {
					if (this.id) {
						// 编辑
						res = await this.$http.put(`rest/contests/${this.id}`, this.model)
						console.log(res.data)
						this.$router.push('/contests/list')
						this.$message({
							type: 'success',
							message: '保存成功'

						})
					} else {
						//新建
						this.model.editor_id = this.$store.state.user.id
						this.model.check = '未审核'
						res = await this.$http.post('rest/contests', this.model)
						this.form = {
							logo: ""
						};
						console.log(res.data)
						this.$router.push('/contests/list')
						this.$message({
							type: 'success',
							message: '上传成功'
						})
					}
				}
			},
			async pass() {
				this.model.check = "通过审核"
				await this.$http.put(`rest/contests/${this.id}`, this.model)
				this.$router.push('/contests/list')
				this.$message({
					type: 'success',
					message: '审核通过！'
				})
			},
			async fail() {
				this.model.check = "审核未通过"
				await this.$http.put(`rest/contests/${this.id}`, this.model)
				this.$router.push('/contests/list')
				this.$message({
					type: 'warning',
					message: '审核不通过！'
				})
			},
			async fetch() {
				const res = await this.$http.get(`rest/contests/${this.id}`)
				this.model = res.data
			},
			async fetchParents() {
				const res = await this.$http.get(`rest/contests`)
				this.parents = res.data
			},
			
			// 验证文件合法性
			beforeUpload (file) {
			const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
			const isLt2M = file.size / 1024 / 1024 < 2
			if (!isJPG) {
				this.$message.error('上传头像图片只能是 JPG 格式!')
			}
			if (!isLt2M) {
				this.$message.error('上传头像图片大小不能超过 2MB!')
			}
			return isJPG && isLt2M
			}
		},
		created() {
			this.id && this.fetch()
		},
		computed: {
			//这里需要把store动态的数据放到computed里面才会同步更新视图
            getPicture(){
				console.log(this.$store.state.carousel_picture)
                return this.$store.state.carousel_picture
            },
			getUploading(){
				console.log(this.$store.state.uploading)
                return this.$store.state.uploading
            }
		},
		watch:{
			getPicture(n){
				this.model.logo = n
				console.log(n)
			},
			getUploading(newInfo){
				this.uploading = newInfo.status
				this.percentage = newInfo.percentage
				console.log(this.percentage)
			}
		}
	}
</script>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		width:auto;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}

</style>
